-- Tags: long, no-tsan, no-asan, no-msan, no-ubsan
SET max_block_size = 65536;
SET min_joined_block_size_rows = 65536;
SET min_joined_block_size_bytes = 512000;
SET enable_lazy_columns_replication = 0;
-- Setting a big number to avoid limiting by the number of rows
SET max_joined_block_size_rows = 100000000;

SET query_plan_join_swap_table = false;

-- { echo ON }

{% for join_kind in ['INNER', 'LEFT', 'RIGHT', 'FULL OUTER'] -%}
{% for join_algorithm in ['hash', 'parallel_hash'] -%}

SELECT if(max(bs) > 65000, 'ok', toString(max(bs)))
FROM
(
    SELECT blockSize() AS bs
    FROM
    (
        SELECT v1, v2
        FROM
        (
            SELECT
                number AS n,
                rightPad(toString(number), 64, '_') AS v1,
                bitAnd(number, 7) AS k
            FROM numbers(65536)
        ) AS l
        {{ join_kind }} JOIN
        (
            SELECT bitAnd(number, 7) AS k,
            rightPad(toString(number), 64, '_') AS v2
            FROM numbers(8 * 32)
        ) AS r ON l.k = r.k
    )
)
SETTINGS max_joined_block_size_bytes = '10Mi', join_algorithm = '{{ join_algorithm }}';

SELECT DISTINCT if (blockSize() as bs < 1000, 'ok', toString(bs))
FROM
(
    SELECT v
    FROM
    (
        SELECT
            number AS n,
            rightPad(toString(number), 1024, '_') AS v,
            bitAnd(number, 7) AS k
        FROM numbers(65536)
    ) AS l
    {{ join_kind }} JOIN
    (
        SELECT bitAnd(number, 7) AS k
        FROM numbers(8 * 32)
    ) AS r ON l.k = r.k
)
SETTINGS max_joined_block_size_bytes = '1Mi', join_algorithm='{{ join_algorithm }}';

SELECT DISTINCT if (blockSize() as bs < 1000, 'ok', toString(bs))
FROM
(
    SELECT v
    FROM
    (
        SELECT
            number AS n,
            bitAnd(number, 7) AS k
        FROM numbers(65536)
    ) AS l
    {{ join_kind }} JOIN
    (
        SELECT
            bitAnd(number, 7) AS k,
            rightPad(toString(number), 1024, '_') AS v
        FROM numbers(8 * 32)
    ) AS r ON l.k = r.k
)
SETTINGS max_joined_block_size_bytes = '1Mi', join_algorithm='{{ join_algorithm }}';

SELECT if(max(bs) > 65000, 'ok', toString(max(bs)))
FROM
(
    SELECT blockSize() AS bs
    FROM
    (
        SELECT v
        FROM
        (
            SELECT
                number AS n,
                rightPad(toString(number), 1024, '_') AS v,
                bitAnd(number, 7) AS k
            FROM numbers(65536)
        ) AS l
        {{ join_kind }} JOIN
        (
            SELECT bitAnd(number, 7) AS k
            FROM numbers(8 * 32)
        ) AS r ON l.k = r.k
    )
)
SETTINGS max_joined_block_size_bytes = '1Gi', join_algorithm = '{{ join_algorithm }}';

SELECT if(max(bs) > 65000, 'ok', toString(max(bs)))
FROM
(
    SELECT blockSize() AS bs
    FROM
    (
        SELECT v
        FROM
        (
            SELECT
                number AS n,
                bitAnd(number, 7) AS k
            FROM numbers(65536)
        ) AS l
        {{ join_kind }} JOIN
        (
            SELECT
                bitAnd(number, 7) AS k,
                rightPad(toString(number), 1024, '_') AS v
            FROM numbers(8 * 32)
        ) AS r ON l.k = r.k
    )
)
SETTINGS max_joined_block_size_bytes = '1Gi', join_algorithm = '{{ join_algorithm }}';

{% endfor -%}
{% endfor -%}

SELECT if(max(bs) >= ((128 * 128) * 4), 'ok', toString(max(bs)))
FROM
(
    SELECT blockSize() AS bs
    FROM
    (
        SELECT
            v1,
            v2
        FROM
        (
            SELECT rightPad(toString(number), 10, '_') AS v1
            FROM numbers(128 * 4)
        ) AS l
        CROSS JOIN
        (
            SELECT rightPad(toString(number), 10, '_') AS v2
            FROM numbers(128 * 4)
        ) AS r
    )
)
SETTINGS max_block_size = 128, max_joined_block_size_bytes = '10Mi';

SELECT DISTINCT if((blockSize() AS bs) < 100000, 'ok', toString(bs))
FROM
(
    SELECT blockSize() AS bs
    FROM
    (
        SELECT
            v1,
            v2
        FROM
        (
            SELECT rightPad(toString(number), 128, '_') AS v1
            FROM numbers(128 * 4)
        ) AS l
        CROSS JOIN
        (
            SELECT rightPad(toString(number), 10, '_') AS v2
            FROM numbers(128 * 4)
        ) AS r
    )
)
SETTINGS max_block_size = 128, max_joined_block_size_bytes = '10Mi';

SELECT DISTINCT if((blockSize() AS bs) < 100000, 'ok', toString(bs))
FROM
(
    SELECT blockSize() AS bs
    FROM
    (
        SELECT
            v1,
            v2
        FROM
        (
            SELECT rightPad(toString(number), 10, '_') AS v1
            FROM numbers(128 * 4)
        ) AS l
        CROSS JOIN
        (
            SELECT rightPad(toString(number), 128, '_') AS v2
            FROM numbers(128 * 4)
        ) AS r
    )
)
SETTINGS max_block_size = 128, max_joined_block_size_bytes = '10Mi';
